<?php
	class Courses extends CI_Model{

	 	function __construct(){
        	parent::__construct();
    	}

			/**
			 * 发布视频课程
			 * @param  [type] $course_id [description]
			 * @return [type]            [description]
			 */
			public function publish_course_video($school_id,$user_id,$data)
			{
				//开始事务
		    $this->db->trans_start();

				$course = array(
		      'uid' => $user_id,
		      'school_id' => $school_id,
		      'teacher_id' => $data['teacher_id'],
		      'cate_id' => $data['cate_id'],
		      'subject_id' => !empty($data['subject_id']) ? $data['subject_id'] : 0,
		      'type' => 1,
		      'title' => $data['title'],
		      'course_img' => !empty($data['img_url'])?$data['img_url'][0]:'',
		      'course_imgs' => !empty($data['img_url'])?json_encode($data['img_url']):'',
		      'detail' => $data['detail'],
		      'price' => $data['price'],
		      'course_time' => $data['course_time'],
		      'class_number' => 0,
		      'effective_time' => $data['effective_time'],
		      'suitable' => $data['suitable'],
		      'learn_what' => $data['learn_what'],
		      'click_num' => rand(0,1000),
		      'status' => 1,
		      'sales' => 0,
		      'is_recomend' => $data['is_recomend'],
		      /*'haoping' => 0,
		      'zhongping' => 0,
		      'chaping' => 0,*/
		      'comment_count' => 0,
		      'add_time' => time(),
		    );
			//添加课程
			$this->db->insert('course',$course);
			$course_id = $this->db->insert_id();
			if($course_id){
				//添加上架信息
        if(!isset($data['is_free'])){
            $data['is_free'] = 0;
        }
				$shelves = array(
					'uid' => $user_id,
					'cate_id' => $data['cate_id'],
					'course_id' => $course_id,
					'teacher_id' => $data['teacher_id'],
					'school_id' => $school_id,
					'type' => 1,
					'fx_price' => $data['price'],
					'commission' => $data['commission'],
                    'is_free' => $data['is_free'],
					'is_fenxiao' => 0,
					'status' => 1,
					'sort' => 0,
					'add_time' => time(),
				);
 				$this->db->insert('course_shelves',$shelves);
			}

			$this->db->trans_complete();
	       return $course_id;
		}

    	function get_subject(){
    		$sql = "SELECT * FROM {$this->db->dbprefix}b_subject ";
    		$result = $this->db->query($sql)->result_array();
    		return $result;
    	}

        /**
         * 获取课程分类，默认不取面授课分类
         * @param  integer $type [description]
         * @return [type]        [description]
         */
        function get_cate($type=1){
            $school_id = (int)$this->user_data['school_id'];
            $and_where = "and type=$type";
            if($type==0){
                $and_where = "and type!=2";//不取面授课分类
            }else{
                $and_where = "and type=$type";
            }
            $sql = "select * from {$this->db->dbprefix}course_cate where school_id = $school_id $and_where";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }

        function get_cate_subject_info($id = 0){
            $sql = "SELECT * from {$this->db->dbprefix}course_cate_subject where id = $id";
            return $this->db->query($sql)->row_array();
        }


        function get_course_chapter($id){
            $sql = "SELECT pid from {$this->db->dbprefix}course_chapter where id = $id";
            $result = $this->db->query($sql)->row_array();
            if(isset($result['pid']) && $result['pid'] > 0){
                $this->db->where('id',$result['pid']);
                $result = $this->db->delete('course_chapter');
                if($result){
                    return true;
                }else{
                    return false;
                }
            }else{
                return true;
            }
        }


        function get_teacher($school_id = 0){
            $sql = "SELECT * FROM {$this->db->dbprefix}o2o_teachers where school_id = $school_id";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }

        /**
         * 网课分类管理
         * @param  [type] $search [description]
         * @param  [type] $type   [description]
         * @param  [type] $offset [description]
         * @param  [type] $limit  [description]
         * @return [type]         [description]
         */
        function get_cate_list($search,$type,$offset,$limit){

            $school_id = (int)$this->user_data['school_id'];
            if(!$search){
                $where = "school_id=$school_id";
            }else{
                $where = "school_id=$school_id and name like '%".$search."%'";
            }
            if($type > 0){
                $where .= " and type = ".$type;
            }else{
                $where .= " and type != 2";//不取面授课的分类
            }

            $sql = "select * from {$this->db->dbprefix}course_cate where  ".$where." order by sort asc limit $limit,$offset";
            $result = $this->db->query($sql)->result_array();

            $sql = "select count(id) as count from {$this->db->dbprefix}course_cate where ".$where." order by sort asc";
            $count = $this->db->query($sql)->row_array();
            return  array('result' => $result, 'count'=>$count['count']);
        }

        /**
         * 网课科目管理
         * @param  [type] $search [description]
         * @param  [type] $type   [description]
         * @param  [type] $offset [description]
         * @param  [type] $limit  [description]
         * @return [type]         [description]
         */
        function get_cate_subject_list($search,$cate_id,$type,$offset,$limit){

            $school_id = (int)$this->user_data['school_id'];
            if(!$search){
                $where = "sub.school_id=$school_id";
            }else{
                $where = "sub.school_id=$school_id and name like '%".$search."%'";
            }
            if($cate_id >0){
                $where .= " and sub.pid = ".$cate_id;
            }

            if($type > 0){
                $where .= " and sub.type = ".$type;
            }else{
                $where .= " and sub.type != 2";//不取面授课的分类
            }

            $sql = "select sub.*,cate.name as category_name from {$this->db->dbprefix}course_cate_subject sub left join {$this->db->dbprefix}course_cate cate on cate.id=sub.pid  where  ".$where." order by sub.id desc limit $limit,$offset";
            $result = $this->db->query($sql)->result_array();

            $sql_count = "select count(id) as count from {$this->db->dbprefix}course_cate_subject sub where ".$where." order by sort asc";
            $count = $this->db->query($sql_count)->row_array();
            return  array('result' => $result, 'count'=>$count['count']);
        }


        function get_cate_info($id = 0){
            $sql = "SELECT * from {$this->db->dbprefix}course_cate where id = $id";
            return $this->db->query($sql)->row_array();
        }


        function get_course_info($id = 0){
            $sql = "SELECT c.*,cs.is_free,cs.commission from {$this->db->dbprefix}course as c left join {$this->db->dbprefix}course_shelves as cs on(c.id = cs.course_id)  where c.type = 1 and c.id = $id and c.school_id=".$this->user_data['school_id'];
            return $this->db->query($sql)->row_array();
        }


        function check_is_sj($id = 0,$uid = 0){
            $sql = "SELECT * from {$this->db->dbprefix}course_shelves where course_id=$id and school_id = $uid";
            return $this->db->query($sql)->row_array();
        }


        function get_chapter_list($id){
            $sql = "SELECT * from {$this->db->dbprefix}course_chapter where pid = 0 and course_id = $id";
            $result = $this->db->query($sql)->result_array();
            if($result){
                foreach ($result as $key => $value) {
					$result[$key]['v_name']='';
                    $sql = "SELECT cc.*,v.title as v_name from {$this->db->dbprefix}course_chapter cc left join {$this->db->dbprefix}video as v on(cc.vid = v.id)  where cc.pid = ".$value['id']." and cc.course_id = $id";
                    $results = $this->db->query($sql)->result_array();
                    $result[$key]['child'] = $results;
                }
            }
            return $result;
        }


        function get_course_cate_subject($id){
            $id = (int)$id;
            $sql = "SELECT * from {$this->db->dbprefix}course_cate_subject where pid = $id and school_id=".$this->user_data['school_id'];
            $results = $this->db->query($sql)->result_array();
            return $results;
        }

        function get_system_cate(){

            $sql = "select * from {$this->db->dbprefix}course_cate where school_id = 0 and type = 1";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }

        function get_school_cate(){

            $sql = "select * from {$this->db->dbprefix}course_cate where school_id = '{$this->user_data['school_id']}' and type = 1";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }


        function get_cate_subject($id){
            $sql = "SELECT * from {$this->db->dbprefix}course_cate_subject where pid = $id";
            return $this->db->query($sql)->result_array();

        }

        function get_course_comment_list($course_id, $limit = 0, $offset = 10){
            $sql = "SELECT SQL_CALC_FOUND_ROWS c.*,s.name AS student_name from {$this->db->dbprefix}course_comment AS c ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}o2o_school_student AS s ON(s.user_id=c.uid) ";
            $sql .= "WHERE c.top_id='{$course_id}' ORDER BY c.id DESC LIMIT $limit, $offset ";
            $result = $this->db->query($sql)->result_array();
            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
            return array("total_count" => $row_num['row_num'], "result" => $result);
        }
	}
